﻿#include "NCTDialogLayUpOwned.h"
#include "ui_NCTDialogLayUpOwned1.h"
#include "ui_NCTDialogLayUpOwned2.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTRegularTrimLayer.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTResistive.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTSeptum.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsNCTPerforated.h"
#include <QMessageBox>
#include <QRadioButton>
namespace GUI
{
    NCTDialogLayUpOwned::NCTDialogLayUpOwned(Acoustics::FITKAcousticsNCTAbstractLayObj *data)
        :AbstractMaterialDialog(Create, nullptr),_data(data)
    {
        if (!_data)return;
        _ntcType = _data->getNCTLayUpType();

        switch (_ntcType) 
        {
            case Acoustics::FITKAcousticsNCTAbstractLayObj::Foam:    
            case Acoustics::FITKAcousticsNCTAbstractLayObj::Fiber:  
            case Acoustics::FITKAcousticsNCTAbstractLayObj::Gap:    
            case Acoustics::FITKAcousticsNCTAbstractLayObj::Panel: {
                // 泡沫、缝隙、实体和纤维类型
                _ui1 = new Ui::NCTDialogLayUpOwned1;
                _ui1->setupUi(this);
                _ui1->stackedWidget->setCurrentIndex(0);
                break;
            }
            case Acoustics::FITKAcousticsNCTAbstractLayObj::Septum: {
                // 隔离类型
                _ui1 = new Ui::NCTDialogLayUpOwned1;
                _ui1->setupUi(this);
                _ui1->stackedWidget->setCurrentIndex(2);
                break;
            }
            case Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Perforated: {
                // 多孔类型
                _ui2 = new Ui::NCTDialogLayUpOwned2;
                _ui2->setupUi(this);
                _ui2->radioButton_Rigid->setChecked(true);
                _ui2->radioButton_Geometry->setChecked(true);
                _ui2->lineEdit_Perforated_Density->setEnabled(false);
                _ui2->comboBox_Solid_Foam->setEnabled(false);
                _ui2->lineEdit_Perforated_FlowResistivity->setEnabled(false);
                _ui2->lineEdit_Perforated_Porpsity->setEnabled(false);
                break;
            }
            case Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Resistive: {
                // 阻尼类型
                _ui1 = new Ui::NCTDialogLayUpOwned1;
                _ui1->setupUi(this);
                _ui1->stackedWidget->setCurrentIndex(1);
                break;
            }
            default: break;
        }

        this->init1();
        this->init2();
    }
    NCTDialogLayUpOwned::~NCTDialogLayUpOwned()
    {
        if (_ui1) delete _ui1;
        if (_ui2) delete _ui2;
    }
    void NCTDialogLayUpOwned::init1()
    {
        if (!_ui1)return;
        if (_ntcType== Acoustics::FITKAcousticsNCTAbstractLayObj::Perforated)return;

        switch (_ntcType)
        {
            case Acoustics::FITKAcousticsNCTAbstractLayObj::Foam: {
                _ui1->label_RegularTrimLayer_LossFactor->hide();
                _ui1->lineEdit_RegularTrimLayer_LossFactor->hide();             
                initComboBox(_ui1->comboBox_RegularTrimLayer_Solid,1);
                initComboBox(_ui1->comboBox_RegularTrimLayer_Fluid,4);
                break;
            }
            case Acoustics::FITKAcousticsNCTAbstractLayObj::Fiber: {
                _ui1->label_RegularTrimLayer_LossFactor->hide();
                _ui1->lineEdit_RegularTrimLayer_LossFactor->hide();
                initComboBox(_ui1->comboBox_RegularTrimLayer_Solid, 2);
                initComboBox(_ui1->comboBox_RegularTrimLayer_Fluid, 4);
                break;
            }
            case Acoustics::FITKAcousticsNCTAbstractLayObj::Gap: {
                _ui1->comboBox_RegularTrimLayer_Solid->setEnabled(false);
                initComboBox(_ui1->comboBox_RegularTrimLayer_Fluid, 4);
                break;
            }
            case Acoustics::FITKAcousticsNCTAbstractLayObj::Panel: {
                _ui1->comboBox_RegularTrimLayer_Fluid->setEnabled(false);
                initComboBox(_ui1->comboBox_RegularTrimLayer_Solid, 3);
                break;
            }
            //Septum  Resistive
            default: break;      
        }
        init1Data();
    }

    void NCTDialogLayUpOwned::init1Data()
    {
        if (!_ui1)return;
        if (_ntcType == Acoustics::FITKAcousticsNCTAbstractLayObj::Perforated)return;
        if (_ntcType == Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Foam ||
            _ntcType == Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Gap ||
            _ntcType == Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Panel ||
            _ntcType == Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Fiber)
        {
            auto data = dynamic_cast<Acoustics::FITKAcousticsNCTRegularTrimLayer*>(_data);
            if (!data)return;
            //获取流体材料id
            int fluidMaterialID = data->getFluidMaterialID();
            int index1 = _ui1->comboBox_RegularTrimLayer_Fluid->findData(fluidMaterialID);
            _ui1->comboBox_RegularTrimLayer_Fluid->setCurrentIndex(index1);
            //获取固体材料id
            int solidMaterialID = data->getSolidMaterialID();
            int index2 = _ui1->comboBox_RegularTrimLayer_Solid->findData(solidMaterialID);
            _ui1->comboBox_RegularTrimLayer_Solid->setCurrentIndex(index2);
            _ui1->lineEdit_RegularTrimLayer_LossFactor->setText(QString::number(data->getLossFactor()));
            _ui1->lineEdit_RegularTrimLayer_Thickness->setText(QString::number(data->getThickness()));
        }
        else if (_ntcType == Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Septum) {
            auto data = dynamic_cast<Acoustics::FITKAcousticsNCTSeptum*>(_data);
            if (!data)return;
            _ui1->lineEdit_Septum_SurfaceMass->setText(QString::number(data->getMassPerUnitArea()));
            _ui1->lineEdit_Septum_Thickness->setText(QString::number(data->getThickness()));
        }
        else if (_ntcType == Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Resistive) {
            auto data = dynamic_cast<Acoustics::FITKAcousticsNCTResistive*>(_data);
            if (!data)return;
            _ui1->lineEdit_Resistive_FlowResistivity->setText(QString::number(data->getFlowResistivity()));
            _ui1->lineEdit_Resistive_Density->setText(QString::number(data->getDensity()));
            _ui1->lineEdit_Resistive_Thickness->setText(QString::number(data->getThickness()));
        }
    }

    void NCTDialogLayUpOwned::init2()
    {
        if (!_ui2)return;
        if (_ntcType != Acoustics::FITKAcousticsNCTAbstractLayObj::Perforated)return;
        auto data = dynamic_cast<Acoustics::FITKAcousticsNCTPerforated*>(_data);
        if (!data)return;

        initComboBox(_ui2->comboBox_Solid_Foam, 1);
        initComboBox(_ui2->comboBox_Solid_Foam, 2);
        initComboBox(_ui2->comboBox_Perforated_Fluid, 4);
        _ui2->lineEdit_Perforated_Density->setText(QString::number(data->getDensity()));
        _ui2->lineEdit_Perforated_AreaRation->setText(QString::number(data->getAreaRatio()));
        _ui2->lineEdit_Perforated_Radius->setText(QString::number(data->getRadius()));
        _ui2->lineEdit_Perforated_FlowResistivity->setText(QString::number(data->getFlowResistivity()));
        _ui2->lineEdit_Perforated_Porpsity->setText(QString::number(data->getPorosity()));

        int solidMaterialID = data->getMaterialID();
        int index1 = _ui2->comboBox_Solid_Foam->findData(solidMaterialID);
        _ui2->comboBox_Solid_Foam->setCurrentIndex(index1);

        int fluidMaterialID = data->getfluidMaterialID();
        int index2 = _ui2->comboBox_Solid_Foam->findData(fluidMaterialID);
        _ui2->comboBox_Solid_Foam->setCurrentIndex(index2);

        _ui2->lineEdit_Perforated_Thickness->setText(QString::number(data->getThickness()));

        Acoustics::FITKAcousticsNCTPerforated::NCTSolidType solidType = data->getSolidType();
        if (solidType == Acoustics::FITKAcousticsNCTPerforated::NCTSolidType::Rigid)
            _ui2->radioButton_Rigid->setChecked(true);
        else if (solidType == Acoustics::FITKAcousticsNCTPerforated::NCTSolidType::FoamOrFiber)
            _ui2->radioButton_Foam->setChecked(true);
        else if (solidType == Acoustics::FITKAcousticsNCTPerforated::NCTSolidType::LimpDensity)
            _ui2->radioButton_Limp->setChecked(true);

        Acoustics::FITKAcousticsNCTPerforated::PerforationType perforationType = data->getPerforationType();
        if (perforationType == Acoustics::FITKAcousticsNCTPerforated::PerforationType::Geometry)
            _ui2->radioButton_Geometry->setChecked(true);
        else if (perforationType == Acoustics::FITKAcousticsNCTPerforated::PerforationType::BiotProperty)
            _ui2->radioButton_BiotProperties->setChecked(true);
    }

    void NCTDialogLayUpOwned::initComboBox(QComboBox *box, int type)
    {
        if (!_matMgr)return;
        QList<Acoustics::FITKAcousticsAbsMaterial*> list{};
        if (type == 1) 
            list = _matMgr->getAcousticsMaterials(Acoustics::FITKAcousticsAbsMaterial::Foam);      
        else if (type == 2) 
            list = _matMgr->getAcousticsMaterials(Acoustics::FITKAcousticsAbsMaterial::Fiber);
        else if (type == 3) 
            list = _matMgr->getAcousticsMaterials(Acoustics::FITKAcousticsAbsMaterial::ISOSolid);
        else if (type == 4) 
            list = _matMgr->getAcousticsMaterials(Acoustics::FITKAcousticsAbsMaterial::Fluid);
        if (list.isEmpty()) return;
        for (auto mate : list) {
            box->addItem(mate->getDataObjectName(), mate->getDataObjectID());
        }
    }

    bool NCTDialogLayUpOwned::checkInput()
    {
        //检查数据合法性
        QList<QLineEdit*> checkUi;
        if(_ui1){
            checkUi << _ui1->lineEdit_RegularTrimLayer_LossFactor << _ui1->lineEdit_RegularTrimLayer_Thickness 
                << _ui1->lineEdit_Septum_SurfaceMass<< _ui1->lineEdit_Septum_Thickness << _ui1->lineEdit_Resistive_FlowResistivity 
                << _ui1->lineEdit_Resistive_Density << _ui1->lineEdit_Resistive_Thickness;
        }
        if (_ui2) {
            checkUi << _ui2->lineEdit_Perforated_Density << _ui2->lineEdit_Perforated_AreaRation
                << _ui2->lineEdit_Perforated_Radius << _ui2->lineEdit_Perforated_FlowResistivity 
                << _ui2->lineEdit_Perforated_Porpsity<< _ui2->lineEdit_Perforated_Thickness ;
        }
        for (QLineEdit* e : checkUi)
        {
            if (e == nullptr) continue;
            QString text = e->text().trimmed();
            bool ok = false;
            if(text!="")text.toDouble(&ok);
            if (!ok)
            {
                QMessageBox::critical(nullptr, tr("Critical"), tr("Please enter a number."));
                return false;
            }
        }
        return true;
    }

    void NCTDialogLayUpOwned::on_pushButton_OK_clicked()
    {
        if (!this->checkInput()) return;
        if (!_ui1)return;

        switch (_ntcType)
        {
        case Acoustics::FITKAcousticsNCTAbstractLayObj::Foam: 
        case Acoustics::FITKAcousticsNCTAbstractLayObj::Fiber: 
        case Acoustics::FITKAcousticsNCTAbstractLayObj::Gap: 
        case Acoustics::FITKAcousticsNCTAbstractLayObj::Panel: {
            auto data = dynamic_cast<Acoustics::FITKAcousticsNCTRegularTrimLayer*>(_data);
            if (!data)return;
            //设置流体id
            data->setFluidMaterialID(_ui1->comboBox_RegularTrimLayer_Fluid->currentData().toInt());
            //设置固体id
            data->setSolidMaterialID(_ui1->comboBox_RegularTrimLayer_Solid->currentData().toInt());
            double lossFactor = _ui1->lineEdit_RegularTrimLayer_LossFactor->text().toDouble();
            data->setLossFactor(lossFactor);
            data->setThickness(_ui1->lineEdit_RegularTrimLayer_Thickness->text().toDouble());
            data->initType();
            break;
        }
        case Acoustics::FITKAcousticsNCTAbstractLayObj::Septum: {
            auto data = dynamic_cast<Acoustics::FITKAcousticsNCTSeptum*>(_data);
            data->setSurfaceMass(_ui1->lineEdit_Septum_SurfaceMass->text().toDouble());
            data->setThickness(_ui1->lineEdit_Septum_Thickness->text().toDouble());
            break;
        }
        case Acoustics::FITKAcousticsNCTAbstractLayObj::NCTLayUpType::Resistive: {
            auto data = dynamic_cast<Acoustics::FITKAcousticsNCTResistive*>(_data);
            data->setFlowResistivity(_ui1->lineEdit_Resistive_FlowResistivity->text().toDouble());
            data->setDensity(_ui1->lineEdit_Resistive_Density->text().toDouble());
            data->setThickness(_ui1->lineEdit_Resistive_Thickness->text().toDouble());
            break;
        }
        //Septum  Resistive
        default: break;
        }
        this->accept();
    }

    void NCTDialogLayUpOwned::on_pushButton_Cancel_clicked()
    {
        this->reject();
    }

    void NCTDialogLayUpOwned::on_pushButton_OKPerforated_clicked()
    {
        if (!this->checkInput()) return;
        if (!_ui2)return;
        auto data = dynamic_cast<Acoustics::FITKAcousticsNCTPerforated*>(_data);

        data->setThickness(_ui2->lineEdit_Perforated_Thickness->text().toDouble());
        data->setDensity(_ui2->lineEdit_Perforated_Density->text().toDouble());
        data->setAreaRatio(_ui2->lineEdit_Perforated_AreaRation->text().toDouble());
        data->setRadius(_ui2->lineEdit_Perforated_Radius->text().toDouble());
        data->setFlowResistivity(_ui2->lineEdit_Perforated_FlowResistivity->text().toDouble());
        data->setPorosity(_ui2->lineEdit_Perforated_Porpsity->text().toDouble());      
        data->setfluidMaterialID(_ui2->comboBox_Perforated_Fluid->currentData().toInt());
        if (_ui2->comboBox_Solid_Foam->isEnabled())
            data->setMaterialID(_ui2->comboBox_Solid_Foam->currentData().toInt());
        else
            data->setMaterialID(-1);

        if (_ui2->radioButton_Rigid->isChecked())
            data->setSolidType(Acoustics::FITKAcousticsNCTPerforated::NCTSolidType::Rigid);
        else if (_ui2->radioButton_Foam->isChecked())
            data->setSolidType(Acoustics::FITKAcousticsNCTPerforated::NCTSolidType::FoamOrFiber);
        else if (_ui2->radioButton_Limp->isChecked())
            data->setSolidType(Acoustics::FITKAcousticsNCTPerforated::NCTSolidType::LimpDensity);

        if (_ui2->radioButton_Geometry->isChecked())
            data->setPerforationType(Acoustics::FITKAcousticsNCTPerforated::PerforationType::Geometry);
        else if (_ui2->radioButton_BiotProperties->isChecked())
            data->setPerforationType(Acoustics::FITKAcousticsNCTPerforated::PerforationType::BiotProperty);
        this->accept();
    }

    void NCTDialogLayUpOwned::on_pushButton_CancelPerforated_clicked()
    {
        this->reject();
    }
    void NCTDialogLayUpOwned::on_radioButton_Limp_toggled(bool checked)
    {
        if (checked == true)
            _ui2->lineEdit_Perforated_Density->setEnabled(true);
        else
            _ui2->lineEdit_Perforated_Density->setEnabled(false);
    }
    void NCTDialogLayUpOwned::on_radioButton_Foam_toggled(bool checked)
    {
        if (checked == true)
            _ui2->comboBox_Solid_Foam->setEnabled(true);
        else
            _ui2->comboBox_Solid_Foam->setEnabled(false);
    }
    void NCTDialogLayUpOwned::on_radioButton_Geometry_toggled(bool checked)
    {
        if (checked == true) {
            _ui2->lineEdit_Perforated_AreaRation->setEnabled(true);
            _ui2->lineEdit_Perforated_Radius->setEnabled(true);
        }
        else {
            _ui2->lineEdit_Perforated_AreaRation->setEnabled(false);
            _ui2->lineEdit_Perforated_Radius->setEnabled(false);
        }
    }
    void NCTDialogLayUpOwned::on_radioButton_BiotProperties_toggled(bool checked)
    {
        if (checked == true) {
            _ui2->lineEdit_Perforated_FlowResistivity->setEnabled(true);
            _ui2->lineEdit_Perforated_Porpsity->setEnabled(true);
        }
        else {
            _ui2->lineEdit_Perforated_FlowResistivity->setEnabled(false);
            _ui2->lineEdit_Perforated_Porpsity->setEnabled(false);
        }
    }
}